// pages/search/search.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history:[],
    shop: [],
    page: 1,
    page_size: 5,
    no_data: 0,
    show: true,// 是否已经到达底部
    word:'',
    have:true,
    over: false
  }, 
  onConfirm(e){
    console.log(e)
    this.setData({
      word: e.detail.value
    })
  },
  // 搜索
  seach() {
    let that = this,
    word = this.data.word;
    that.saveSearchHistory(word);
    wh.Post('/api/mall.goods/search', {key: word},function(res){
      console.log('商品',res)
      if(res.length==0){
        that.setData({
          have: false,
        });
      }else{
        that.setData({
          have: true,
        });
      }
      that.setData({
        show: false,
        shop:res,
        over: true
      });
    })
  },
// 保存历史记录
  saveSearchHistory(word) {

    let history = wh.getData('searchHistory') || [];
    if (history.indexOf(word) === -1) {
      if (history.length < 9) {
        history.unshift(word);
      } else {
        history.splice(8,1);
        history.unshift(word);
      }
      wh.setData('searchHistory', history)
    }
  },
// 点击历史记录
  itemTap(e) {
    let that = this;
    let word = e.target.dataset.word;
    wh.Post('/api/mall.goods/search', { key: word }, function (res) {
    console.log('商品',res)
      if (res.length == 0) {
        that.setData({
          have: false,
        });
      } else {
        that.setData({
          have: true,
        });
      }
      that.setData({
        word: word,
        show: false,
        shop: res,
        over: true
      });
    })
  },
// 清楚历史记录
  clearSearchHistorys() {
    this.setData({
      history: []
    });
    wh.setData('searchHistory', []);
  },
  onLoad: function (options) {
    this.setData({
      history: wh.getData('searchHistory') || []
    });
  },
  // 跳转至商品详情
  toGoods(e) {
    let that = this,
      id = e.currentTarget.dataset.id,
      act_type = e.currentTarget.dataset.act_type;
    if (act_type == 1) {
      wx.navigateTo({
        url: '../details/details?goods_id=' + id,
      })
    } else if (act_type == 2) {
      wx.navigateTo({
        url: '../cutDetails/cutDetails?bargainId=' + id,
      })
    } else if (act_type == 3) {
      wx.navigateTo({
        url: '../killDetails/killDetails?seckill_id=' + id,
      })
    } else if (act_type == 4) {
      wx.navigateTo({
        url: '../groupDetails/groupDetails?group_id=' + id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示 
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      // this.lists()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})